/** 出入金概览*/
var crAmount1 = 0;
var crAmount2 = 0;
var crAmount3 = 0;
var crAmount4 = 0;
var crAmount5 = 0;

var rAmount1 = 0;
var rAmount2 = 0;
var rAmount3 = 0;
var rAmount4 = 0;
var rAmount5 = 0;

/** 客户概览*/
var custOverView1 = 0;
var custOverView2 = 0;
var custOverView3 = 0;
var custOverView4 = 0;
var custOverView5 = 0;

/** 订单概览*/
var orderOverView1 = 0;
var orderOverView2 = 0;
var orderOverView3 = 0;
var orderOverView4 = 0;
var orderOverView5 = 0;

var PATH_PREFIX ="";

function setPathPrefix(path){
	PATH_PREFIX =  path;
}


$(function(){
	WASP_WIDGET.triggerIRadio("input[type='radio'][name='timeQ']");
	WASP_WIDGET.triggerIRadio("input[type='radio'][name='timeQ']",function(){
		initProductFyDetail();
	},function(){
		//initProductFyDetail();
	});
	/*
	initCustRegisDetail();*/
	queryClearIsOver();
	
	showPredictTime();
	
	bindRadioboxDivClick();
	
	initCustRegisDetail('2');
});

var serialNo = "123456";

function showPredictTime(){
	var timestamp = new Date().getTime();
	timestamp = timestamp + 1.5 * 60 * 60 * 1000;
	var date  = new Date(timestamp);
	var hours = date.getHours();
	var minute = date.getMinutes();
	minute = minute < 10 ? "0" + minute : minute;
	var dateStr = "23" + ":" + "25";
	var predictTime = $("#predictTime");
	predictTime.html(dateStr);
}


function bindRadioboxDivClick(){
	/*$("div[id='titleSelector']").find("span:not(:first)").each(function(index){*/
	$("div[id='titleSelector']").find("span.subSelector").each(function(index){
		if($(this).attr('reportFrequency') == '2'){
			$(this).toggleClass("font-color-white");
		}
		
		$(this).on('click',function(){
			$('#titleSelector .font-color-white').each(function(index,element){
				$(element).removeClass('font-color-white');
			});
			$(this).toggleClass("font-color-white");
			var reportFrequency = $(this).attr("reportFrequency");
			initCustRegisDetail(reportFrequency);
		});
	});
}


function initCustRegisDetail(reportFreQuency){
	var timeQ= reportFreQuency;
	
	$.ajax({
		url : registerPath+"/getEchartsData.do",
		data : {
			'type':timeQ
		},
		dataType : "json",
		cache : false,
		async: false,
		error : function(textStatus, errorThrown) {
			ctools.alert("系统ajax交互错误: ",'',"warning");
		},
		success : function(data) {
			if(data != null){
				crAmount1 = parseFloat(data.crAmount1 == ""?"0":data.crAmount1);
				crAmount2 = parseFloat(data.crAmount2 == ""?"0":data.crAmount2);
				crAmount3 = parseFloat(data.crAmount3 == ""?"0":data.crAmount3);
				crAmount4 = parseFloat(data.crAmount4 == ""?"0":data.crAmount4);
				crAmount5 = parseFloat(data.crAmount5 == ""?"0":data.crAmount5);
				
				rAmount1 = parseFloat(data.rAmount1 == ""?"0":data.rAmount1);
				rAmount2 = parseFloat(data.rAmount2 == ""?"0":data.rAmount2);
				rAmount3 = parseFloat(data.rAmount3 == ""?"0":data.rAmount3);
				rAmount4 = parseFloat(data.rAmount4 == ""?"0":data.rAmount4);
				rAmount5 = parseFloat(data.rAmount5 == ""?"0":data.rAmount5);
				
				
				
				custOverView1 = parseFloat(data.custOverView1 == ""?"0":data.custOverView1);
				custOverView2 = parseFloat(data.custOverView2 == ""?"0":data.custOverView2);
				custOverView3 = parseFloat(data.custOverView3 == ""?"0":data.custOverView3) ;
				custOverView4 = parseFloat(data.custOverView4 == ""?"0":data.custOverView4);
				custOverView5 = parseFloat(data.custOverView5 == ""?"0":data.custOverView5);
				
				orderOverView1 = parseFloat(data.orderOverView1 == ""?"0":data.orderOverView1);
				orderOverView2 = parseFloat(data.orderOverView2 == ""?"0":data.orderOverView2);
				orderOverView3 = parseFloat(data.orderOverView3 == ""?"0":data.orderOverView3);
				orderOverView4 = parseFloat(data.orderOverView4 == ""?"0":data.orderOverView4);
				orderOverView5 = parseFloat(data.orderOverView5 == ""?"0":data.orderOverView5);
			}
		}
	});
	
	var xDateData1 = getWeekDate();
	var xDateData2 = getRecentMonthDate();
	
	var capDataShow1 = [ {value:crAmount1, name: '划款'},
				      {value:crAmount2, name: '划款'},
				      {value:crAmount3, name: '划款'},
				      {value:crAmount4, name: '划款'},
				      {value:crAmount5, name: '划款'}];
	
	var capInDataShow1 = [ {value:rAmount1, name: '收款'},
	      {value:rAmount2, name: '收款'},
	      {value:rAmount3, name: '收款'},
	      {value:rAmount4, name: '收款'},
	      {value:rAmount5, name: '收款'}];

	
	var capDataShow2 = [ {value:crAmount1, name: '划款'},
		{value:crAmount2, name: '划款'},
		{value:crAmount3, name: '划款'}];
	
	var capInDataShow2 = [ {value:rAmount1, name: '收款'},
				      {value:rAmount2, name: '收款'},
				      {value:rAmount3, name: '收款'}];
	
	var custDataShow1 = [ {value:custOverView1, name: ''},
	      {value:custOverView2, name: ''},
	      {value:custOverView3, name: ''},
	      {value:custOverView4, name: ''},
	      {value:custOverView5, name: ''}];
	
	var custDataShow2 = [ {value:custOverView1, name: ''},
	      {value:custOverView2, name: ''},
	      {value:custOverView3, name: ''}
	      ];
	
	var orderDataShow1 = [ {value:orderOverView1, name: ''},
	      {value:orderOverView2, name: ''},
	      {value:orderOverView3, name: ''},
	      {value:orderOverView4, name: ''},
	      {value:orderOverView5, name: ''}];
	
	var orderDataShow2 = [ {value:orderOverView1, name: ''},
	      {value:orderOverView2, name: ''},
	      {value:orderOverView3, name: ''}];
	
	//柱状图 出入金概览
	var barChart = echarts.init(document.getElementById('barChart1')); 
	barChart.clear();  
	barChart.showLoading({text: '正在努力的读取数据中...'}); 
	var data = ['收款'];
	/*var option1 = timeQ == '2'?  capitalOption1: capitalOption2;*/
	var option1 = {
		legend: [
			{
				orient: 'horizontal',
				top:'2%',  
				right:'5%', 
				itemGap: 20,
				data: ['划款'],
			},
			{
	            orient: 'horizontal',
	            top:'6%',  
	            right:'5%', 
	            itemGap: 20,
	            data: ['收款'],
			}
		],
		title: {
        text: '出入金概览',
        x:'center',
        y:'top'
    },
    xAxis: {
        type: 'category',
        axisTick:{
        	//是否显示坐标轴刻度
        	show:false,
        	alignWithLabel:false
        },
        /*axisLine:{
        	// 轴末端是否显示(箭头)图标
        	symbol:['none', 'arrow']
        },*/
        data:timeQ == '2' ? xDateData1 : xDateData2
    },
    yAxis: {
    	name: '金额(万元)',
        type: 'value',
        //是否显示分隔线
        splitLine:{
        	show:false
        },
        axisTick:{
        	//是否显示坐标轴刻度
        	show:false,
        	alignWithLabel:false
        },
        //是否显示刻度标签
        axisLabel: {
        	show: false
        },
       /*,
        axisLine:{
        	// 轴末端是否显示(箭头)图标
        	symbol:['none', 'arrow']
        }*/
    },
    series: [{
    	name:'划款',
    	label:{
    		normal:{
    			show:true,
    			position:'top'
    		}
    	},
        data: timeQ == '2' ? capDataShow1 : capDataShow2,
        type: 'bar',
        barWidth : timeQ == '2'? 25:40,
        //color: ['#ff9966', '#ff6633', '#55aa77','#ff9999'],
        // 设置柱体的颜色
        itemStyle:{
        	normal:{
        		color:function(params){
        			var colorList = ['#FFA34A', '#FFA34A', '#FFA34A','#FFA34A','#FFA34A'];
        			return colorList[params.dataIndex]
        		}
        	}
        }
    },
    {
    	name:'收款',
    	label:{
    		normal:{
    			show:true,
    			position:'top'
    		}
    	},
        data: timeQ == '2' ? capInDataShow1 : capInDataShow2,
        type: 'bar',
        barWidth :timeQ == '2'? 25:40,
        //color: ['#ff9966', '#ff6633', '#55aa77','#ff9999'],
        // 设置柱体的颜色
        itemStyle:{
        	normal:{
        		color:function(params){
        			var colorList = ['#64CAF5', '#64CAF5', '#64CAF5','#64CAF5','#64CAF5'];
        			return colorList[params.dataIndex]
        		}
        	}
        }
    }
    
    ]
};
		
	//柱状图 客户概览
	var barChart2 = echarts.init(document.getElementById('barChart2')); 
	barChart2.clear();  
	barChart2.showLoading({text: '正在努力的读取数据中...'}); 
	var option2 = {
			title: {
				text: '客户概览',
				x:'center',
		        y:'top'
			},
			xAxis: {
				type: 'category',
				axisTick:{
					//是否显示坐标轴刻度
					show:false,
					alignWithLabel:false
				},
				/*axisLine:{
		        	// 轴末端是否显示(箭头)图标
		        	symbol:['none', 'arrow']
		        },*/
				data:timeQ == '2' ? xDateData1 : xDateData2
			},
			yAxis: {
				name: '注册人数',
				type: 'value',
				//是否显示分隔线
				splitLine:{
					show:false
				},
				axisTick:{
					//是否显示坐标轴刻度
					show:false,
					alignWithLabel:false
				},
				//是否显示刻度标签
				axisLabel: {
					show: false
				},
				/*,
		        axisLine:{
		        	// 轴末端是否显示(箭头)图标
		        	symbol:['none', 'arrow']
		        }*/
			},
			series: [{
				label:{
					normal:{
						show:true,
						position:'top'
					}
				},
				data:timeQ == '2' ? custDataShow1 : custDataShow2,
				type: 'bar',
				//color: ['#ff9966', '#ff6633', '#55aa77','#ff9999'],
				// 设置柱体的颜色
				barWidth :timeQ == '2'? 25:40,
				itemStyle:{
					normal:{
						color:function(params){
							var colorList = ['#FFA34A', '#64CAF5', '#FF8DA9','#B98FEC','#00ACC1'];
							return colorList[params.dataIndex]
						}
					}
				}
			}
			]
	};
	//条形图表 实际
	var barChart3 = echarts.init(document.getElementById('barChart3')); 
	barChart3.clear();  
	barChart3.showLoading({text: '正在努力的读取数据中...'}); 
	var option3 = {
			title: {
		        text: '订单概览',
		        x:'center',
		        y:'top'
		    },
		    xAxis: {
		        type: 'category',
		        axisTick:{
		        	//是否显示坐标轴刻度
		        	show:false,
		        	alignWithLabel:false
		        },
		        /*axisLine:{
		        	// 轴末端是否显示(箭头)图标
		        	symbol:['none', 'arrow']
		        },*/
		        data:timeQ == '2' ? xDateData1 : xDateData2
		    },
		    yAxis: {
		    	name: '订单数量',
		        type: 'value',
		        //是否显示分隔线
		        splitLine:{
		        	show:false
		        },
		        axisTick:{
		        	//是否显示坐标轴刻度
		        	show:false,
		        	alignWithLabel:false
		        },
		        //是否显示刻度标签
		        axisLabel: {
		        	show: false
		        },
		       /*,
		        axisLine:{
		        	// 轴末端是否显示(箭头)图标
		        	symbol:['none', 'arrow']
		        }*/
		    },
		    series: [{
		    	label:{
		    		normal:{
		    			show:true,
		    			position:'top'
		    		}
		    	},
		    	data:timeQ == '2' ? orderDataShow1 : orderDataShow2,
		        type: 'bar',
		        barWidth :timeQ == '2'? 25:40,
		        //color: ['#ff9966', '#ff6633', '#55aa77','#ff9999'],
		        // 设置柱体的颜色
		        itemStyle:{
		        	normal:{
		        		color:function(params){
		        			var colorList = ['#FFA34A', '#64CAF5', '#FF8DA9','#B98FEC','#00ACC1'];
		        			return colorList[params.dataIndex]
		        		}
		        	}
		        }
		    }]
		};
	
	//载入图表  
	loadDrugs(barChart,option1);
	loadDrugs(barChart2,option2);
	loadDrugs(barChart3,option3);
}

function loadDrugs(element,option) {  
	element.setOption(option, true);  
	element.hideLoading();
	window.onresize = function () {
		element.resize();
    };
} 

var flag = true;

function queryClearIsOver(){
	
	$.ajax({
		async:true,
		url:PATH_PREFIX + 'queryClearStep.do',
		type:"post",
		dataType:'json',
		data:{
			serialNo : serialNo
		},
		success:function(data){
			for(var i=0;i<data.groupList.length;i++){
				var vo =data.groupList[i];
				if(vo.clearstepCode == '10005' && vo.clearStatus == 'S'){
					flag = false;
				}
			}
			
		}
	});	
	
	if(flag){
		setTimeout("queryClearIsOver()",5*1000);
	}else{
		$("#downloadBtn1").removeAttr('disabled');
		$("#downloadBtn2").removeAttr('disabled');
	}
}

function down(fileType) {
	ctools.confirm({title : "您确定要下载此文件吗?", text: ""},
			function(isConfirm){
				if(isConfirm){
					window.location.href=encodeURI(registerPath+"/downloadFile.do?sp[fileType]="+fileType);
				}
	});
}

function getWeekDate(){
	var d=new Date(),
	day=d.getDay(),
	date=d.getDate();
	if(day==1){
		return d;
	}else if(day==0){
		d.setDate(date-6);
	}else{
		d.setDate(date-day+1);
	}
	var arr=[];
	for(var i=0; i<5; i++){
		arr.push((d.getMonth()+1)+'月'+d.getDate()+'日');
		d.setDate(d.getDate()+1);
	}
	return arr;
}
function getRecentMonthDate(){
	var monthOfYear = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
	var date = new Date();
	var nearThree = [];
	var month = date.getMonth();
	nearThree.push(monthOfYear[month]);
	for (var i = 1; i <= 2; i++) {
	    date.setMonth(month - i);
	    nearThree.unshift(monthOfYear[date.getMonth()]);
	}
	return nearThree;
}